﻿using Newtonsoft.Json;
using Station.Inspect.SuaModel;
using Station.Inspect.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace Vision.Common
{
    /// <summary>
    /// 元件库
    /// </summary>
    public class NodeLib : IXmlSerializable
    {
        [XmlAttribute]
        public int ID { get; set; }
        public string Name { get => $"元件{ID}"; }
        public SizeF size { get; set; }
        [XmlAttribute]
        public float Angle { get; set; }
        [JsonIgnore]
        public Bitmap Image { get; set; }
        /// <summary>
        /// 二维码位置
        /// </summary>
        public bool IsMatrixPos { get; set; } = false;
        /// <summary>
        /// 明码位置
        /// </summary>
        public bool IsLablePos { get; set; } = false;
        /// <summary>
        /// BadMark位置
        /// </summary>
        public bool IsBadMarkPos { get; set; } = false;
        /// <summary>
        /// S算子
        /// </summary>
        public bool IsUseSeg { get; set; } = false;
        /// <summary>
        /// S+C算子
        /// </summary>
        public bool IsUseSegClass { get; set; } = false;
        /// <summary>
        /// T算子
        /// </summary>
        public bool IsUseDefect { get; set; } = false;
        /// <summary>
        /// T+C算子
        /// </summary>
        public bool IsUseDefectClass { get; set; } = false;
        /// <summary>
        /// S阈值
        /// </summary>
        public float SegThreshold { get; set; } = 0;
        /// <summary>
        /// S+C阈值
        /// </summary>
        public float SegClassThreshold { get; set; } = 0;
        /// <summary>
        /// S最小尺寸
        /// </summary>
        public float SegMinSize { get; set; } = 0;
        /// <summary>
        /// S最小面积
        /// </summary>
        public float SegMinArea { get; set; } = 0;
        /// <summary>
        /// T阈值
        /// </summary>
        public float DefectThreshold { get; set; } = 0;
        /// <summary>
        /// T+C阈值
        /// </summary>
        public float DefectClassThreshold { get; set; } = 0;
        /// <summary>
        /// T最小尺寸
        /// </summary>
        public float DefectMinSize { get; set; } = 0;
        /// <summary>
        /// T最小面积
        /// </summary>
        public float DefectMinArea { get; set; } = 0;
        /// <summary>
        /// 模型Cell ID
        /// </summary>
        public int ModelCellID { get; set; } = 0;
        /// <summary>
        /// 模型最小得分
        /// </summary>
        public float ModelMinScore { get; set; } = 0;
        /// <summary>
        /// 模型偏移范围
        /// </summary>
        public float ModelRange { get; set; } = 0;

        public bool IsUseMark { get; set; } = false;
        [JsonIgnore]
        public Bitmap ModelImage { get; set; } = null;
        [JsonIgnore]
        //public ModelHNcc ModelMark { get; set; } = null;
        public ModelScaledShape ModelMark { get; set; } = null;

        private List<NodeCell> nodeCells = new List<NodeCell>();
        
        public List<NodeCell> NodeCells { get => nodeCells; set => nodeCells=value; }
        
        private List<NodeElement> elements = new List<NodeElement>();
        [JsonIgnore]
        public List<NodeElement> Elements { get => elements; }

        public NodeLib()
        {

        }
        public NodeLib(int ID,SizeF size,float Angle)
        {
            this.ID = ID;
            this.size = size;
            this.Angle = Angle;
        }
        public int GetMaxCellID()
        {
            int maxID = 0;
            if (nodeCells.Count > 0)
                maxID = nodeCells.Max(p => p.ID);
            return maxID;
        }

        /// <summary>
        /// 关联，仅元件类可以调用
        /// </summary>
        /// <param name="node"></param>
        public void Attatch(NodeElement node)
        {
            if (elements.Contains(node) == false)
            {
                elements.Add(node);
                if (node.Lib != this)
                    node.Lib = this;
            }    
        }
        /// <summary>
        /// 取消关联，仅元件类可以调用
        /// </summary>
        /// <param name="node"></param>
        public void Dettatch(NodeElement node)
        {
            if (elements.Contains(node))
            {
                elements.Remove(node);
                if(node.Lib==this)
                {
                    node.Lib = null;
                }
                //node.Lib = null;
            }   
        }

        public void RemoveAllElements()
        {
            while(elements.Count>0)
            {
                Dettatch(elements[0]);
            }  
        }

        public void UpdateAllElements()
        {
            foreach (var item in elements)
            {
                if(item.Lib == this)
                    item.Lib = this;
            }
        }

        public XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            ID =Convert.ToInt32(reader.GetAttribute("ID"));
            Angle = Convert.ToSingle(reader.GetAttribute("Angle"));
            string strSize = reader.GetAttribute("Size");
            float width = 0;
            float height = 0;
            if (string.IsNullOrEmpty(strSize))
            {
                if (reader.ReadToDescendant("size"))
                {
                    reader.ReadStartElement();
                    width = reader.ReadElementContentAsFloat("Width", "");
                    height = reader.ReadElementContentAsFloat("Height", "");
                    reader.Read();//EndSize
                }
            }
            else
            {
                string[] strSizes = strSize.Split(',');
                if(strSizes.Length >=2)
                {
                    width = Convert.ToSingle(strSizes[0]);
                    height = Convert.ToSingle(strSizes[1]);
                }
            }
            size = new SizeF(width, height);
            reader.Read();//EndNodeLib
        }

        public void WriteXml(XmlWriter writer)
        {
            //writer.WriteStartElement("NodeLib");
            writer.WriteAttributeString("ID", ID.ToString());
            writer.WriteAttributeString("Angle", Angle.ToString());
            writer.WriteAttributeString("Size", $"{size.Width},{size.Height}");
            //writer.WriteEndElement();
        }
    }

    //public static class NodeLibManager
    //{
    //    public static List<NodeLib> nodeLibs = new List<NodeLib>();
    //    public static int GetMaxID()
    //    {
    //        return nodeLibs.Max(p => p.ID);
    //        //int maxID = 0;
    //        //foreach(var item in nodeLibs)
    //        //{
    //        //    if (maxID < item.ID)
    //        //        maxID = item.ID;
    //        //}
    //        //return maxID;
    //    }
    //    public static NodeLib Find(int ID)
    //    {
    //        return nodeLibs.First(p => p.ID == ID);
    //        //foreach (var item in nodeLibs)
    //        //{
    //        //    if (item.ID == ID)
    //        //        return item;
    //        //}
    //        //return null;
    //    }
    //}
}
